package com.incar.chuxing.utils.bos;

import android.content.Context;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.BceCredentials;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.ThreadPool;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by michaelpi on 2018/6/20.
 */

public class UploadFile {
    private Context mContext;
    private static UploadFile instance;

    public static UploadFile getInstance() {
        if (instance == null) {
            instance = new UploadFile();
        }
        return instance;
    }

    public UploadFile() {
    }

    public void uploadPicToBos(Context context, String fileName) {
        mContext = context;
        // 1. get pic params from ui: file name, file location uri etc
        // 2. send params to app server and get sts, bucket name and region
        // 3. upload selected pic to bos with sts etc, which bos client needs
        File file = new File(mContext.getExternalFilesDir("Log").getPath() + File.separator + fileName);
        if (!file.exists()) {
            Logger.d("日志文件不存在");
            //ToastUtils.makeText(mContext, "上传失败", ToastUtils.LENGTH_LONG).show();
            return;
        }
        BceCredentials credentials = new DefaultBceCredentials("233a6499eee24777826d0cd04815f419", "74a359feb1464025aa92cfbcd90d6c58");
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(credentials);
        final BosClient client = new BosClient(config);
        ThreadPool.execute(() -> {
            try {
                Logger.d("日志" + "开始上传日志");
                SimpleDateFormat sp = new SimpleDateFormat("MMddHHmm");
                //上传Object
                PutObjectResponse putObjectFromFileResponse = client.putObject("userlogs",
                        ((MyApplication) MyApplication.getInstance()).getUid() + " " + sp.format(new Date()) + fileName,
                        file);
                Logger.d("日志" + "上传成功");
            } catch (BceServiceException e) {
                Logger.d("日志", "日志上传失败Service" + e.getErrorCode() + "  " + e.getMessage());
            } catch (BceClientException e) {
                Logger.d("日志", "日志上传失败Client" + e.getMessage());
            }
        });
    }

    public void uploadFileToBos(Context context, File file) {
        mContext = context;
        if (!file.exists()) {
            Logger.d("文件不存在");
            //ToastUtils.makeText(mContext, "上传失败", ToastUtils.LENGTH_LONG).show();
            return;
        }
        BceCredentials credentials = new DefaultBceCredentials("233a6499eee24777826d0cd04815f419", "74a359feb1464025aa92cfbcd90d6c58");
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(credentials);
        final BosClient client = new BosClient(config);
        ThreadPool.execute(() -> {
            try {
                Logger.d("日志" + "开始上传日志");
                //上传Object
                client.putObject("userlogs", file.getName(), file);
                Logger.d("文件" + "上传成功");
            } catch (BceServiceException e) {
                Logger.d("文件", "文件上传失败Service" + e.getErrorCode() + "  " + e.getMessage());
            } catch (BceClientException e) {
                Logger.d("文件", "文件上传失败Client" + e.getMessage());
            }
        });

    }

    /*
     * Java文件操作 获取不带扩展名的文件名
     */
    public String getFileNameNoEx(File nameFile) {
        String filename = nameFile.getName();
        if (filename.length() > 0) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
